/*
 * Copyright (C) 2009 Rob Cash
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * LocalFileChooserWizardPanel.java
 */

package org.robcash.apps.messagepopup.view.wizard;

import org.robcash.apps.messagepopup.view.components.LocalFileChooser;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.robcash.apps.messagepopup.vo.MessageStoreFactory;
import org.robcash.apps.messagepopup.vo.impl.LocalFileMessageStoreLocation;
import org.robcash.commons.swing.wizard.Wizard;
import org.robcash.commons.swing.wizard.impl.BaseWizardPanel;
import org.robcash.commons.swing.wizard.model.WizardModel;

/**
 *
 * @author Rob
 */
public class LocalFileChooserWizardPanel
        extends BaseWizardPanel<LocalFileChooser>
        implements ChangeListener {

    private LocalFileChooser chooser;

    public LocalFileChooserWizardPanel(String objectId) {
        super(objectId);
        chooser = new LocalFileChooser();
        chooser.addChangeListener(this);
        setComponent(chooser);
    }

    @Override
    public String getNextPanelId() {
        return WizardModel.NEXT;
    }

    @Override
    public boolean canGoForward() {
        return chooser.getSelectedFile() != null &&
                chooser.getSelectedFile().isFile();
    }

    /**
     * Indicates that the wizard panel is about to be hidden. This
     * implementation saves the message store location.
     * @param wizard Wizard that is about to hide this panel
     * @param saveWork Indicates whether or not work should be saved
     */
    @Override
    public void aboutToHide(Wizard wizard, boolean saveWork) {
        if ( saveWork ) {
            LocalFileMessageStoreLocation storeLocation =
                    new LocalFileMessageStoreLocation(chooser.getSelectedFile());
            MessageStoreFactory factory = MessageStoreFactory.newInstance();
            ((SubscriptionModel) wizard.getModel()).setMessageStore(
                    factory.newMessageStore(storeLocation));
        }
        super.aboutToHide(wizard, saveWork);
    }

    /**
     * Receive notification that something has changed.
     * @param e Change event
     */
    public void stateChanged(ChangeEvent e) {
        if ( e.getSource().equals(chooser) ) {
            fireChangeListenerStateChanged(new ChangeEvent(this));
        }
    }

}
